package leetcode.weekly.week322;

import java.util.Arrays;

//Solution2Test
public class Solution2 {

	public long dividePlayers(int[] skill) {
		Arrays.sort(skill);
		int len = skill.length;
		int sum = skill[0] + skill[len - 1];
		long ans = skill[0] * skill[len - 1];
		for (int i = 1, j = len - 2; i <= (len - 2) >> 1; i++, j--) {
			int n1 = skill[i];
			int n2 = skill[j];
			if (n1 + n2 != sum) {
				return -1;
			}
			ans += 1l * n1 * n2;
		}
		return ans;
	}
}
